<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\WtPaid;
use App\Admin\Repositories\ReceivePaymentBody;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;

class ReceivePaymentController extends AdminController
{
    protected $title = "收款";
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ReceivePaymentBody(), function (Grid $grid) {
            $grid->model()->orderByDesc("id");
            $grid->combine('原币', ['debit', 'credit']);
            $grid->combine('本位币', ['natdebit', 'natcredit']);
            $grid->id->sortable();
            $grid->pay_at('确认收款')->display(function ($value) {
                return $value ? "已收款" : "<span style='color: dodgerblue'>未收款</span>";
            });
            $grid->DocItem;
            $grid->Resume;
            $grid->acctcode;
            $grid->acctname;
            $grid->AcctFullName;
            $grid->dtcurrency;
            $grid->dtexchange_rate;
            $grid->debit;
            $grid->credit;
            $grid->natdebit;
            $grid->natcredit;
            $grid->cv;
            $grid->accessary;
            $grid->Originaldatetime;

            // 确认支付按钮
            $grid->actions(new WtPaid());
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });

            $grid->selector(
                function (Grid\Tools\Selector $selector) {
                    // 是否支付 规格筛选器
                    $selector->selectOne(
                        'pay_at',
                        '是否收款',
                        [
                            '已收款',
                            '未收款',
                            '所有',
                        ],
                        function ($query, $value) {
                            if ($value == 0) {
                                $query->whereNotNull('pay_at');
                            } else if ($value == 1) {
                                $query->whereNull('pay_at');
                            }
                        }
                    );
                }
            );

            $grid->disableViewButton();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
            $grid->disableBatchDelete();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ReceivePaymentBody(), function (Show $show) {
            $show->id;
            $show->DocCode;
            $show->DocItem;
            $show->Resume;
            $show->Rowid;
            $show->acctcode;
            $show->acctname;
            $show->AcctFullName;
            $show->dtcurrency;
            $show->dtexchange_rate;
            $show->debit;
            $show->credit;
            $show->natdebit;
            $show->natcredit;
            $show->refrowid;
            $show->cv;
            $show->cv1;
            $show->cv1name;
            $show->cv2;
            $show->cv2name;
            $show->cv3;
            $show->cv3name;
            $show->cv4;
            $show->cv4name;
            $show->cv5;
            $show->cv5name;
            $show->ismodifyflag;
            $show->accessary;
            $show->Originaldatetime;
            $show->fluxid;
            $show->fluxname;
            $show->opendigit;
            $show->dtLinkcode;
            $show->dtlinkformid;
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ReceivePaymentBody(), function (Form $form) {
            $form->display('id');
            $form->text('DocCode');
            $form->text('DocItem');
            $form->text('Resume');
            $form->text('Rowid');
            $form->text('acctcode');
            $form->text('acctname');
            $form->text('AcctFullName');
            $form->text('dtcurrency');
            $form->text('dtexchange_rate');
            $form->text('debit');
            $form->text('credit');
            $form->text('natdebit');
            $form->text('natcredit');
            $form->text('refrowid');
            $form->text('cv');
            $form->text('cv1');
            $form->text('cv1name');
            $form->text('cv2');
            $form->text('cv2name');
            $form->text('cv3');
            $form->text('cv3name');
            $form->text('cv4');
            $form->text('cv4name');
            $form->text('cv5');
            $form->text('cv5name');
            $form->text('ismodifyflag');
            $form->text('accessary');
            $form->text('Originaldatetime');
            $form->text('fluxid');
            $form->text('fluxname');
            $form->text('opendigit');
            $form->text('dtLinkcode');
            $form->text('dtlinkformid');
        });
    }
}
